package com.link2room.blaster.MNSworker;

import com.aliyun.mns.model.Message;
import com.link2room.blaster.dto.innerUse.InmailBaseDto;
import com.link2room.blaster.facade.InMailFacadeService;
import com.link2room.common.mns.Worker;
import com.link2room.common.util.JacksonUtil;
import com.link2room.blaster.dto.param.inMail.InmailCreateParamDto;
import com.link2room.blaster.dto.param.inMail.SendInmailIdParamDto;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * Created by LN on 2017/6/28.14:28
 */
@Component("inmailSendWorker")
public class InmailSendWorker implements Worker {
    @Resource
    private InMailFacadeService inMailFacadeService;

    @Override
    public boolean exec(Message message) throws Exception {
        if (message.getDequeueCount() > 1) {
            return true;
        }
        InmailBaseDto baseDto = null;
        try {
            String inmailStr = message.getMessageBody();
            InmailBaseDto inmailDto = JacksonUtil.jsonToBean(inmailStr, InmailBaseDto.class);
            InmailCreateParamDto inmailCreateParamDto = new InmailCreateParamDto();
            inmailCreateParamDto.setLhotelGroupCode(inmailDto.getLhotelGroupCode());
            inmailCreateParamDto.setLhotelCode(inmailDto.getLhotelCode());
            inmailCreateParamDto.setInmalBaseDto(inmailDto);
            baseDto = inMailFacadeService.createInmail(inmailCreateParamDto);
        } catch (Exception e) {
            return false;
        }
        try {
            SendInmailIdParamDto paramDto = new SendInmailIdParamDto();
            paramDto.setInmailBaseDto(baseDto);
            inMailFacadeService.sendInmailId(paramDto);
        } catch (Exception e) {
            throw e;
        } finally {
            return true;

        }
    }
}
